package com.edu.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.common.utils.JwtUtils;
import com.edu.common.utils.MD5;
import com.edu.common.utils.R;
import com.edu.eduservicebase.exception.GuliException;
import com.edu.ucenterservice.entity.UcenterMember;
import com.edu.ucenterservice.mapper.UcenterMemberMapper;
import com.edu.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.ucenterservice.vo.LoginVo;
import com.edu.ucenterservice.vo.RegisterVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Member;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-07
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> RedisTemplate;


    /**
     *登录
     * @param loginVo
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        //校验参数
        if (StringUtils.isEmpty(mobile)||
            StringUtils.isEmpty(password)){
            throw new GuliException(20001,"电话号为空!");
        }

        //获取会员!
        UcenterMember member = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
            if (null==member){
                throw new GuliException(20001,"用户信息不存在!");
            }

            //校验密码
         if (!MD5.encrypt(password).equals(member.getPassword())){
             throw  new  GuliException(20001,"密码错误!");
         }

         ////校验是否被禁用
        if (member.getIsDeleted()){
            throw new GuliException(20001,"用户被封禁");
        }

        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());

        return token;
    }

    /**
     * 注册
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册信息，进行校验
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        //校验参数
        if (StringUtils.isEmpty(mobile)||
                StringUtils.isEmpty(code)||
                StringUtils.isEmpty(nickname)||
                StringUtils.isEmpty(password)){
            throw new GuliException(20001,"非法参数!");
        }
        //校验校验验证码
        //从redis获取发送的验证码
        String mobleCode = RedisTemplate.opsForValue().get(mobile);
        if (!code.equals(mobleCode)){
            throw  new GuliException(20001,"验证码错误!");
        }

        //查询数据库是否存在相同的手机号!
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));

        if (count.intValue()>0){
            throw  new GuliException(20001,"手机号已存在!");
        }

        //添加注册信息到数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setNickname(nickname);
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);

        ucenterMember.setAvatar("https://pics6.baidu.com/feed/d833c895d143ad4b0ce9ec42503ac4a7a50f062a.jpeg?token=6cce537ffffc76d4828cbfc156eb578e");

        this.save(ucenterMember);

    }


    /**
     * 解析token
     * @param memberId
     * @return
     */
    @Override
    public RegisterVo getLoginInfo(String memberId) {
        UcenterMember ucenterMember = baseMapper.selectById(memberId);
        RegisterVo registerVo = new RegisterVo();
        BeanUtils.copyProperties(ucenterMember,registerVo);
        return registerVo;
    }

    /**
     * 查询是否存在
     * @param openid
     * @return
     */
    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(queryWrapper);

        return member;
    }


}
